package net.sunniwell.weatherdemo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import net.sunniwell.location.LocateCityBean;
import net.sunniwell.location.LocationUtil;
import net.sunniwell.weather.Weather;
import net.sunniwell.weather.WeatherBean.DataBean;

public class MainActivity extends Activity {

	private Button btn_bean;
	private Button btn_json;
	private Button btn_find;
	private EditText et_bean;
	private EditText et_json;
	private EditText et_find;
	private TextView tv_bean;
	private TextView tv_json;
	private TextView tv_find;
	private TextView tv_current_location;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();

		btn_bean.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					private String weather = "";

					@Override
					public void run() {
						String cityId = et_bean.getText().toString();
						if (TextUtils.isEmpty(cityId)) {
							cityId = "2";
						}
						DataBean data = Weather.getWeatherBean(cityId).getData();
						weather += "城市地理信息==";
						weather += data.getCity().toString();
						weather += "\n";
						weather += "城市天气信息==";
						weather += data.getCondition().toString();
						weather += "\n";
						weather += data.getCity().getName();
						weather += "==";
						weather += data.getCondition().getCondition();
						weather += "\n";
						weather += "温度==";
						weather += data.getCondition().getTemp();
						weather += "℃";
						weather += "\n";
						weather += "湿度==";
						weather += data.getCondition().getHumidity();
						weather += "\n";
						weather += data.getCondition().getWindDir();
						weather += "==";
						weather += data.getCondition().getWindLevel();

						// weather = Weather.getWeatherBean(cityId,
						// 墨迹天气个人申请的appcode).getData().getCondition().toString();
						// weather = Weather.getWeatherBean(cityId,
						// 墨迹天气个人申请的appcode).toString();
						MainActivity.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								tv_bean.setText(weather);
							}
						});

					}
				}).start();
			}
		});
		btn_json.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					private String weather;

					@Override
					public void run() {
						String cityId = et_json.getText().toString();
						if (TextUtils.isEmpty(cityId)) {
							cityId = "2";
						}
						weather = Weather.getWeatherJson(cityId).toString();
						// weather = Weather.getWeatherJson(cityId,
						// 墨迹天气个人申请的appcode).toString();
						MainActivity.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								tv_json.setText(weather);
							}
						});

					}
				}).start();
			}
		});
		btn_find.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					private String weather;

					@Override
					public void run() {
						String city = et_find.getText().toString();
						if (TextUtils.isEmpty(city)) {
							city = "北京";
						}
						weather = getFindCityId(city).toString();
						MainActivity.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								tv_find.setText(weather);
							}
						});

					}
				}).start();
			}
		});
	}

	private void initData() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				LocateCityBean locateCityBean = LocationUtil.locateCityBean();
				double latitude = locateCityBean.getLat();
				double longitude = locateCityBean.getLon();
				final String weatherJson = Weather.getWeatherJson(latitude + "", longitude + "", "");
				MainActivity.this.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						tv_current_location.setText(weatherJson);
					}
				});
			}
		}).start();
	}

	private void initView() {
		btn_bean = (Button) findViewById(R.id.btn_bean);
		btn_json = (Button) findViewById(R.id.btn_json);
		btn_find = (Button) findViewById(R.id.btn_find);
		et_bean = (EditText) findViewById(R.id.et_bean);
		et_json = (EditText) findViewById(R.id.et_json);
		et_find = (EditText) findViewById(R.id.et_find);
		tv_bean = (TextView) findViewById(R.id.tv_bean);
		tv_json = (TextView) findViewById(R.id.tv_json);
		tv_find = (TextView) findViewById(R.id.tv_find);
		tv_current_location = (TextView) findViewById(R.id.tv_current_location);
	}

	public String getFindCityId(String city) {
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(MainActivity.this.getResources().getAssets().open("zpc.zpc"));
			BufferedReader br = new BufferedReader(inputStreamReader);
			String readLine = null;
			StringBuilder sb = new StringBuilder();
			while ((readLine = br.readLine()) != null) {
				if (readLine.contains(city)) {
					sb.append(readLine);
					sb.append("\n");
					sb.append("\n");
				}
			}
			br.close();
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "查询不到此地区";
	}
}
